/**
 * @file     isPalindrome.js
 * @brief    [125. 验证回文串](https://leetcode.cn/problems/valid-palindrome/)
 * @author   Zhu
 * @date     2022-10-22 23:54
 */

/**
 * @param {string} s
 * @return {boolean}
 */
var isPalindrome = function (s) {
    const size = s.length;
    let left = 0;
    let right = size - 1;

    while (left < right) {
        while (left < right && !isValidChar(s[left])) {
            left++;
        }
        while (left < right && !isValidChar(s[right])) {
            right--;
        }

        if (left >= right) break;

        if (s[left].toLocaleLowerCase() === s[right].toLocaleLowerCase()) {
            left++;
            right--;
        } else {
            return false;
        }
    }

    return true;
};

/**
 * @param {stirng} char
 */
function isValidChar(char) {
    return /[a-zA-Z0-9]/.test(char);
}
